package com.yzb.test

import com.yzb.config.FtpConfig
import com.yzb.ftp.FtpProgressListener
import com.yzb.helper.FtpClientHelper
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.context.properties.bind.Binder
import org.springframework.boot.context.properties.source.ConfigurationPropertySources
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.ApplicationContext
import org.springframework.core.env.PropertiesPropertySource
import org.springframework.core.io.ClassPathResource
import org.springframework.test.context.junit4.SpringRunner

/**
 * <br/>
 * @auther huang yu zhi
 * @version 1.0.0* @date 2019-01-25 11:47
 */
@RunWith(SpringRunner)
@SpringBootTest
class Test2 {
    @Autowired
    private ApplicationContext applicationContext

    @Test
    public void test1() {
        def resource = new ClassPathResource('ftp.properties')
        def properties = new Properties()
        properties.load(resource.getInputStream())

        def propertiesPropertySource = new PropertiesPropertySource('ftpClient', properties)
        def binder = new Binder(ConfigurationPropertySources.from(propertiesPropertySource))

        println(binder.bind('ftp', FtpConfig.class).get())
    }

    @Test
    public void test2() {
//        def remoteFile = 'pub/jdk-6u45-windows-x64.exe', localDir = 'd:/tmp'
        def remoteFile = ['pub/CentOS-7-x86_64-Minimal-1804.iso', 'pub/jdk-6u45-windows-x64.exe'], localDir = 'd:/tmp'
//        def remoteFile = ['pub/jdk-6u45-windows-x64.exe'], localDir = 'd:/tmp'
//        def host = '47.105.187.241', userName = 'hyz', userPwd = '123456'
        def host = '192.168.0.115', userName = 'hyz', userPwd = '123'
        remoteFile.each {
            def file = it
            Thread.startDaemon {
                def ftpClient = FtpClientHelper.build(host, userName, userPwd)
                def listener = applicationContext.getBean(FtpProgressListener.class)
                FtpClientHelper.downloadFileToLocalDir(ftpClient, file, localDir, true, listener)
            }
        }
        while (true) {

        }
        //FtpClientHelper.downloadFileToLocalDir(ftpClient, remoteFile, localDir, true, new DefaultFtpProgressListener())
    }
}
